package com.project.dao.impl;

import com.project.bean.EmployeeBean;
import com.project.dao.IEmployeeDao;
import org.springframework.stereotype.Repository;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@Repository
public class EmployeeDaoImpl  extends BaseDao implements IEmployeeDao {
    @Override
    public List<EmployeeBean> findAll() {
        List<EmployeeBean> list = new ArrayList<>();
        this.setConnection();
        try {
            ps = con.prepareStatement("select * from  t_employee");

            rs = ps.executeQuery();

            while(rs.next()){
                list.add( this.getEmployee());
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally{
            this.closeConnection();
        }
        return list;
    }

    @Override
    public void add(EmployeeBean em) {
        this.setConnection();
        try {
            ps = con.prepareStatement("INSERT INTO t_employee(e_name,e_phone,e_money,e_faceImg,e_status) values(?,?,?,?,?)");
            ps.setObject(1,em.getName());
            ps.setObject(2,em.getPhone());
            ps.setObject(3,em.getMoney());
            ps.setObject(4,em.getImgPath());
            ps.setObject(5,em.getStatus());

            ps.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally{
            this.closeConnection();
        }
    }

    @Override
    public void update(Integer id, Integer money) {
        this.setConnection();
        try {
            ps = con.prepareStatement("update t_employee set e_money=? where pk_emId=?");
            ps.setObject(1,money);
            ps.setObject(2,id);

            ps.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally{
            this.closeConnection();
        }
    }

    @Override
    public EmployeeBean findById(Integer id) {
        this.setConnection();
        try {
            ps = con.prepareStatement("select * from  t_employee  where pk_emId=?");
            ps.setObject(1,id);
            rs = ps.executeQuery();

            if(rs.next()){
               return this.getEmployee();
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally{
            this.closeConnection();
        }
        return null;
    }

    private EmployeeBean getEmployee() throws SQLException {
        EmployeeBean em = new EmployeeBean();
        em.setId(rs.getInt("pk_emId"));
        em.setName(rs.getString("e_name"));
        em.setPhone(rs.getString("e_phone"));
        em.setMoney(rs.getInt("e_money"));
        em.setImgPath(rs.getString("e_faceImg"));
        em.setStatus(rs.getString("e_status"));
        return em;
    }

    @Override
    public void dimission(Integer id, String status) {
        this.setConnection();
        try {
            ps = con.prepareStatement("update t_employee set e_status=? where pk_emId=?");
            ps.setObject(1,status);
            ps.setObject(2,id);

            ps.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally{
            this.closeConnection();
        }
    }
}
